package com.java.dao.impl;

import com.java.dao.BOrderDao;
import com.java.domain.BOrder;
import com.java.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.xml.transform.Source;
import java.sql.SQLException;
import java.util.List;

public class BOrderDaoImpl implements BOrderDao {

    private String sql;
    private QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
    @Override
    public List<BOrder> queryBOrder(BOrder bOrder) {
        sql="SELECT o.payment,o.`status`,o.createtime,o.uname,d.*\n" +
            "FROM `order` o,orderdetail d\n" +
            "WHERE o.oid=d.oid\n" +
            "LIMIT ?,?";
        try {
                   // 工具类    sql语句    工具类规定的写法  参数类型 参数的class文件    当前第几页          这一页显示多少条数据
            return runner.query(sql,new BeanListHandler<BOrder>(BOrder.class),bOrder.getOffset(),bOrder.getPageNumber());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public long queryBOrderTotal() {
        sql = "SELECT count(*) FROM `order` o,orderdetail d WHERE o.oid=d.oid";
        try {
            return runner.query(sql,new ScalarHandler<Long>());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delBOrder(String oid) {
        System.out.println(oid);
        sql = "DELETE o,d\n" +
                "FROM `order` o,orderdetail d \n" +
                "WHERE o.oid=d.oid AND o.oid=?";
        try {
            return runner.update(sql,oid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }
}
